*******************
* Constructing HH outcomes for Global South COVID paper
* WB Sample
* 2020-06-23
* last updated: 2020-09-09
* Magdalena Larreboure
*******************

set more off
gl weight hh_weight


use "$dr/KEN3_raw.dta", clear
rename weight hh_weight

*number of HH members
rename s2q_19h_current_num_adults n_adults
rename hhsize n_hhmem
label var n_adults "Number of adults in the household"
label var n_hhmem "Number of household members, including children"


********************************************************************************************************
							******************* Food security *******************
********************************************************************************************************

// - Missed meals
// - Reduced portions


loc fdays s5_3_q7a_hungryadult s5_3_q7b_hungrychild s5_3_q8a_skippedadult s5_3_q8b_skippedchild s5_3_q9b_nofoodchild s5_3_q9a_nofoodadult //how many days out of 7 qs42-44 in paper version
foreach var of varlist `fdays' {
loc name_`var'=substr("`var'", 10, 12)
g f1_d_`name_`var'' =`var'
replace f1_d_`name_`var''=. if `var'==-99

}
label var f1_d_hungryadult "Number of days adults have gone to bed hungry in the past 7 days"
label var f1_d_hungrychild "Number of days children have gone to bed hungry in the past 7 days" //42b. In the past 7 days, how many days have CHILDREN in your household gone to bed hungry?"
label var f1_d_skippedadult "Number of days adults have skipped meals in the past 7 days" //43a. In the past 7 days, how many days have ADULTS in your household skipped meals or cut the amount of meals?
label var f1_d_skippedchild "Number of days children have skipped meals in the past 7 days"
label var f1_d_nofoodadult "Number of days adults have gone the entire day without food" //44a. In the past 7 days, how many days have ADULTS in your household gone entire days without food?
label var f1_d_nofoodchild "Number of days children have gone the entire day without food"


g missedmeals=1 if (f1_d_skippedadult>=1 & f1_d_skippedadult<=7) | (f1_d_skippedchild>=1 & f1_d_skippedchild<=7)
replace missedmeals=1 if (f1_d_nofoodadult>=1 & f1_d_nofoodadult<=7) | (f1_d_nofoodchild>=1 & f1_d_nofoodchild<=7)
replace missedmeals=0 if f1_d_skippedadult==0 & f1_d_skippedchild==0 & f1_d_nofoodadult==0 & f1_d_nofoodchild==0
label var missedmeals "Share of sample missing or cutting meals in the past 7 days"


********************************************************************************************************
							**************** Earnings / Income ****************
********************************************************************************************************

gen ag = s4_q1_agactivity_0 != 1
tab ag
sum s4_q10_agearnings if ag == 1, d // lots of zeroes

// I1: Agricultural earnings: 4.10
g i1=s4_q10_agearnings if !inlist(s4_q10_agearnings,-99, -98, 99)
replace i1=i1/$pppfactor
// replace i1=i1/n_hhmem
replace i1=0 if s4_q1_agactivity_0==1
label var i1 "Agricultural earnings in the past 14 days"


// I2: Self-employment earnings: sum of 4.20 across enterprises -  Over the past 14 days, what was the total profit of this enterprise?
gen selfemp = s4_q11_num_ent > 0 if s4_q11_num_ent >= 0

foreach var of varlist s4_q20_profits_* {
replace `var'=. if inlist(`var',-99,-98,99)
}

forval i=1/2 {
replace s4_q20_profits_`i'=0 if s4_q13_operating_`i'!=1
}

egen i2=rowtotal(s4_q20_profits_*),m
forval i = 1/2 {
	replace i2 = . if s4_q20_profits_`i' == .
}
sum i2 if selfemp == 1, d

replace i2=i2/$pppfactor
// replace i2=i2/n_hhmem
label var i2 "Self-employment earnings in the past 14 days"


// I3: Wage earnings: 4.28
egen nemp =rowtotal(s4c_q23_whoemployed_*),m
replace nemp=0 if s4c_q23_whoemployed__0==1
replace nemp=nemp-1 if s4c_q23_whoemployed__77==1
gen emp = nemp>0

tab emp ag
gen any = emp == 1 | ag == 1 | selfemp == 1
tab any sample, column // 30% of households have no income at all

loc wagehhmemb s4_q28_cashamt_*
foreach var of varlist `wagehhmemb' {
replace `var'= 0 if `var' == .
replace `var'=. if `var'==-99 | `var'==-98  |`var'==99 | `var'==98
replace `var'=0 if s4c_q23_whoemployed__0==1
}

egen i3=rowtotal(`wagehhmemb') , m
foreach var of varlist `wagehhmemb' {
	replace i3 = . if `var' == .
}

replace i3=i3/$pppfactor
label var i3 "Wage earnings in the past 14 days"
g inc2_wages = i3
label var inc2_wages "Total employment earnings in the last 2 weeks"

// I4: Total Household earnings
loc i i1 i2 i3
egen i4=rowtotal(`i'), m
forval i = 1/3 {
	replace i4 = . if i`i' == .
}
label var i4 "Total household earnings in the past 14 days"
g inc3_total = i4
label var inc3_total "Total HH income in the last 2 weeks"

loc inc inc2_wages inc3_total


*** February Income **
************************

** For ag, this is last year **
g i1_pre=s4_q10a_agearningstrend if !inlist(s4_q10_agearnings,-99, -98, 99)
replace i1_pre=i1_pre/$pppfactor
replace i1_pre=0 if s4_q1_agactivity_0==1
label var i1_pre "Agricultural earnings in at the same time last year"


// I2: Self-employment earnings: sum of 4.20 across enterprises -  Over the past 14 days, what was the total profit of this enterprise?
foreach var of varlist s4_q20a_profits_* {
replace `var'=0 if `var' == .
replace `var'=. if inlist(`var',-99,-98,99)
}

egen i2_pre=rowtotal(s4_q20a_profits_*), m
foreach var of varlist s4_q20a_profits_* {
	replace i2_pre = . if `var' == .
}
replace i2_pre=i2_pre/$pppfactor
// replace i2=i2/n_hhmem
label var i2_pre "Self-employment earnings in the a typical 14 day period in February"


// I3: Wage earnings feb: 4.28a
loc wagefeb s4_q28a_cashamt_biweek_*
foreach var of varlist `wagefeb' {
replace `var'=0 if `var'==.
replace `var'=. if `var'==-99 | `var'==-98 | `var'==99 | `var'==98

local loopnum = substr("`var'",-1,1)
replace `var' = 0 if s4_q27a_workbeforemarch_`loopnum' == 0
}

egen i3_pre=rowtotal(`wagefeb') , m
foreach var of varlist `wagefeb' {
	replace i3_pre = . if `var' == .
}

replace i3_pre=i3_pre/$pppfactor
// replace i3=i3/n_hhmem
label var i3_pre "Biweekly wage earnings back in February"
g inc2_wages_pre = i3_pre
label var inc2_wages_pre "Biweekly employment earnings back in February"


// I4: Total pre-period household earnings
loc i i1_pre i2_pre i3_pre
egen i4_pre =rowtotal(`i') , m
label var i4_pre  "Total household earnings in the pre-period (February for non-ag, last year for ag)"
g inc3_total_pre = i4_pre
label var inc3_total_pre "Total HH income in the pre-period (February for non-ag, last year for ag)"


*** Income Drop **
******************
/*
gen inc1_ag_drop = (s4_q10a_agearningstrend > s4_q10_agearnings & !inlist(s4_q10_agearnings,-99,-98,99) & !inlist(s4_q10a_agearningstrend,-99,-98,99)) ///
| s4_q7_plantamt == 3 | s4_q8_harvestamt == 3

gen inc2_selfemp_drop =
*/

gen inc3_total_drop = (inc3_total_pre > inc3_total) & inc3_total != . & inc3_total_pre != .

gen inc3_total_increase = (inc3_total_pre < inc3_total) & inc3_total != . & inc3_total_pre != .


** add in layoffs that weren't replaced **
forval i = 1/15 {
	gen lostjob`i' = s4c_q23_whoemployed_`i'==0 & s4_q33_wholaidoff_`i'==1 & s4_q33_wholaidoff__0 ==0
}

/*
forval i = 1/15 {
	forval j = 1/4 {
		replace lostjob`i' = 0 if s4_q33_wholaidoff_`i' == 1 & inlist(s4_q33c_whenlaidoff_`j',6,7,8) // don't count Before 2020/Jan/Feb layoffs 
		*ML: come back to this. wholaidoff hhmember number doesnt correspond to whenlaidoff hhmember number
	}
}

*** Dissaving ****
******************

// Dissaving: (assets sold) + (loans taken) - Assets/Durables (bought)
g s5_q3c_durables_net=-s5_q3c_durables if s5_q3c_durables!=-99 & s5_q3c_durables!=-98 // Substracting assets/durables bought
replace s6_q2b_loanamt=0 if s6_q2_tookloan==0
replace s6_q1b_soldamt=0 if s6_q1_soldasset=="1"
loc dissaving s6_q1b_soldamt s6_q2b_loanamt s5_q3c_durables_net
foreach var of varlist `dissaving' { // s5_q3c_durables_net included in loop since there are multiple 98 and 99 observations in s5_q3c_durables which is probably an input error
replace `var'=. if `var'==-99
replace `var'=. if `var'==-98
}
egen i7=rowtotal(`dissaving') , m
replace i7=i7/$pppfactor
replace i7=i7/n_hhmem
label var i7 "Dissaving (pc) in the past 14 days"
// Indicator for Dissavings
g dissaved=1 if i7 > 0
replace dissaved = 0 if i7 <= 0
label var dissaved "Household dissaved"
*/

egen lostjobs = rowtotal(lostjob?), m

forval i = 1/4 {
	gen gainjob`i' = s4_q27a_workbeforemarch_`i' == 0
}
egen gainjobs = rowtotal(gainjob?), m

replace inc3_total_drop = 1 if lostjobs > 0 & gainjobs == 0 & lostjobs != .

tab inc3_total_drop
tab inc3_total_drop if inc3_total_pre > 0 | lostjobs > 0

*Adding obs for: closed an enterprise (and didn't re-open / start one).

g startenterprise=1 if s4_q12d_startyear_1==2020 | s4_q12d_startyear_2==2020
replace startenterprise=0 if (inlist(s4_q12d_startmonth_1, 1, 2) & s4_q12d_startyear_1==2020) | (inlist(s4_q12d_startmonth_2, 1, 2) & s4_q12d_startyear_2==2020)

replace inc3_total_drop=1 if (inlist(s4_q13_operating_1, 2, 3, 4) | inlist(s4_q13_operating_2, 2, 3, 4)) & startenterprise!=1


********************************************************************************************************
							******************* Employment *******************
********************************************************************************************************

// - Drop in employment


// 1. Share of employed that lost job since February - emp1_lostjob
g nemployed_now= nemp // 24. Which ADULT household members are currently employed, working for pay?

egen nlaidoff_smarch =rowtotal(s4_q33_wholaidoff_*),m
replace nlaidoff_smarch=0 if s4_q33_wholaidoff__0==1
replace nlaidoff_smarch=nlaidoff_smarch-1 if s4_q33_wholaidoff__77==1

replace nlaidoff_smarch=nlaidoff_smarch-1 if inlist(s4_q33c_whenlaidoff_1,6,7,8) 
replace nlaidoff_smarch=nlaidoff_smarch-1 if inlist(s4_q33c_whenlaidoff_2,6,7,8) 
replace nlaidoff_smarch=nlaidoff_smarch-1 if inlist(s4_q33c_whenlaidoff_3,6,7,8) 
replace nlaidoff_smarch=nlaidoff_smarch-1 if inlist(s4_q33c_whenlaidoff_4,6,7,8) 

g nemployed_march=nemployed_now+nlaidoff_smarch

g emp1_lostjob=nlaidoff_smarch/nemployed_march
label var emp1_lostjob "Share of employed that lost job since February"

*Household level employment drop:

// The Kenya GE Siaya sample calculates a drop in employment at the household level, based on whether anyone in the household has reported being laid off / losing their job
// since February 2020, and not reporting being currently employed.
// This is constructed from the following questions, which collects individual-level data, and which we aggregate to a household level.
// Which ADULT household members are currently employed, working for pay?
// Since January 2020, which ADULTS in your household were laid off / lost their job? If this number is greater than zero, we then collect the month of the layoff. Layoffs occurring in February 2020 or earlier are not counted.

g employmentdrop=cond(lostjobs>gainjobs,1,0) // this would be: the amount of people laid off in the hh is greater than the amount of people who gained a job since march
label var employmentdrop "Employment drop in the household"

replace employmentdrop=. if nemployed_march==0 // to make it conditional on anyone in the household working for wages in Feb 2020

********************************************************************************************************
							******************* Access to markets *******************
********************************************************************************************************

// - Reduction in access to markets


********************************************************************************************************
							******************* NGO or GOV support *******************
********************************************************************************************************
// - Received new NGO or Govt Support

// I5: Support from government, NGOs/community groups and politicians: 5.37a + 5.38a + 5.39a
loc support s7_q3a_value s7_q4a_value s7_q5a_value
loc support2 s7_q3a_value s7_q4a_value
replace s7_q3a_value=0 if s7_q4_govthelp==0
replace s7_q4a_value=0 if s7_q5_ngohelp==0
replace s7_q5a_value=0 if s7_q6_politicianhelp==0
foreach var of varlist `support' {
replace `var'=. if inlist(`var', -99, -98)
}
g i5a=s7_q3a_value/$pppfactor
g i5b=s7_q4a_value/$pppfactor
g i5c=s7_q5a_value/$pppfactor

label var i5a "Support from government"
label var i5b "Support from NGOs/community groups"
label var i5c "Support from politicians"

egen i5=rowtotal(`support2') , m
replace i5=i5/$pppfactor
label var i5 "Support from government and NGOs/community groups (pc) in the past 14 days"

tab i5, m
g anysupport=1 if i5>0 & ~mi(i5)
replace anysupport=0 if i5==0
label var anysupport "Share of sample receiving any support from government and NGOs in the past 14 days"


********************************************************************************************************
							******************* Health care access *******************
********************************************************************************************************

g healthaccess= s9_q7_accesstreatment==0 | inlist(s9_q6_check_fr,2,3) if s9_q7_accesstreatment!=.z | s9_q6_check_fr!=.z  // no access to treatment or reduced frequency for routine check-ups


loc outcomes missedmeals inc3_total_drop employmentdrop anysupport healthaccess inc3_total_increase


 qui sum i4_pre , detail // [aw=$weight]
 g b_inc= cond(i4_pre <= `r(p50)', 1, 0)

 preserve
keep id hh_weight `outcomes' b_inc s2_q9a_county

  save "$da/KEN3_maintable.dta" , replace
restore
 ********************************************************************************************************

 
 g female = s2_q3_gender==1
 g rural = urban==0
 
 g yearseduc=. if s2_q4_educ==-77 // Other
replace yearseduc=0 if s2_q4_educ==0 // No Formal Education
replace yearseduc=2 if s2_q4_educ==1 // Pre-primary
replace yearseduc=8 if s2_q4_educ==2 // Primary
replace yearseduc=9 if s2_q4_educ==3 // Post-primary, vocational
replace yearseduc=12 if s2_q4_educ==4 // Secondary
replace yearseduc=15 if s2_q4_educ==5 // College, middle-level
replace yearseduc=15 if s2_q4_educ==6 // University undergraduate
replace yearseduc=15 if s2_q4_educ==7 // University postgraduate
replace yearseduc=4 if s2_q4_educ==8 // Madrassa/ Duksi

fre s2_q4_educ
g completesecondary =1 if inlist(s2_q4_educ,4,5,6,7)
replace completesecondary=0 if inlist(s2_q4_educ,0,1,2,3,8)


// C1: Total food expenditure in the past 7 days: 4.1.1 + 4.3.Groceries/Food
loc c1 s5_q3a_food s5_q1_consumptionamt // expenditure + own agricultural or pastoral production
replace s5_q3a_food=. if s5_q3a_food==-99
replace s5_q1_consumptionamt=. if s5_q1_consumptionamt==-99
egen c1= rowtotal(`c1') , m
replace c1=c1/$pppfactor
// replace c1=c1/n_hhmem
label var c1 "Total food expenditure in the past 7 days"
g cons3_food=c1
label var cons3_food "Food consumption expenditure"


// C2: Total food consumption in the past 7 days: C1 +  4.1.2a
replace s5_q2a_foodamt=. if s5_q2a_foodamt==-99
replace s5_q2a_foodamt=0 if s5_q2_receivedfood==2
g s5_q2a_foodamt_PPP = s5_q2a_foodamt/$pppfactor 
// replace s5_q2a_foodamt_PPP = s5_q2a_foodamt_PPP/n_hhmem 
loc c2 c1 s5_q2a_foodamt_PPP 
egen c2= rowtotal(`c2') , m
label var c2 "Total food consumption (incl. gifts) in the past 7 days"

// C3: Non food expenditure in the past 7 days: 4.3.Household Items + 4.3.Assets + 4.3.Services + 4.3.Communication + 4.3.Housing + 4.3.Energy + 4.3.Transport + 4.3.Medical

* We ask for past 14 days for b-i
loc nfexp s5_q3b_personal s5_q3c_durables s5_q3d_services s5_q3e_comms s5_q3f_housing s5_q3g_utilities s5_q3h_transport s5_q3i_medical
foreach var of varlist `nfexp' {
replace `var'=. if `var'==-99
replace `var'=`var'/2 
}
egen c3=rowtotal(`nfexp') , m
replace c3=c3/$pppfactor
// replace c3=c3/n_hhmem
label var c3 "Non-food expenditure in the past 7 days"

// C4: Total consumption in the past 7 days: C2 + C3
egen c4=rowtotal(c2 c3) , m
label var c4 "Total consumption in the past 7 days"

g consumption=c4/n_adults



 keep female rural n_hhmem hh_weight yearseduc s2_q4_educ completesecondary consumption 

   save "$da/KEN3_reptable.dta" , replace

